package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.PaperfileMapper;
import com.hbnu.system.model.entity.Paper;
import com.hbnu.system.model.entity.Paperfile;
import com.hbnu.system.service.IPaperService;
import com.hbnu.system.service.IPaperfileService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Paperfile)表服务实现类
 *
 * @author makejava
 * @since 2023-08-15 13:46:42
 */
@Service("paperfileService")
public class PaperfileServiceImpl extends ServiceImpl<PaperfileMapper, Paperfile> implements IPaperfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    IPaperService iPaperService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 新增附件
     * @param paperfile
     * @return
     */
    @Override
    public boolean addPaperfile(Paperfile paperfile) {
        Paper byId = iPaperService.getById(paperfile.getPaperid());
        checkRoleUtil.checkPermissionAchievement(byId.getStatus());
        paperfile.setFiledate(LocalDateTime.now());
        if (byId.getStatus().contains("审核")){
            throw new BaseException("该状态不可提交附件!");
        }
        paperfile.setFiledate(LocalDateTime.now());
        return baseMapper.insert(paperfile)==1;
    }

    @Override
    public Paperfile getPaperfile(String id) {
        Paperfile paperfile = super.getOne(Wrappers.lambdaQuery(Paperfile.class)
                .eq(Paperfile::getFileid,id));
        if(Objects.isNull(paperfile)){
            throw new BaseException("文件编号异常!未查找到该文件信息");
        }
        return paperfile;
    }

    @Override
    public List<Paperfile> pagePaperfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("论文编号为空值");
        }
        List<Paperfile> paperfileList = baseMapper.selectList(Wrappers.lambdaQuery(Paperfile.class)
                .eq(Paperfile::getPaperid,id));
        return paperfileList;
    }

    /**
     * 删除附件
     * @param id
     * @return
     * @throws IOException
     */
    @Override
    public boolean deletPaperfile(String id) throws IOException {
        Paperfile one = super.getOne(Wrappers.lambdaQuery(Paperfile.class)
                .eq(Paperfile::getFileid, id));
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
        Paper paper = iPaperService.getById(one.getPaperid());
        checkRoleUtil.checkPermissionAchievement(paper.getStatus());
        System.out.println("Fileurl="+one.getFileurl());
        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updatePaperfile(Paperfile paperfile) {
        Paper paper = iPaperService.getById(paperfile.getPaperid());
        checkRoleUtil.checkPermissionAchievement(paper.getStatus());
        return baseMapper.updateById(paperfile)==1;
    }


}

